from XlementFitting.FileProcess.JsonFormat import check_unpredicted_json_format
from XlementFitting.FileProcess.Json2Data import read_and_process_json, get_fitting_options
from XlementFitting.FileProcess.Data2Json import process_and_save_json
from XlementFitting import PartialBivariate, GlobalBivariate, LocalBivariate, SingleCycleFitting3
from pathlib import Path
import argparse
import numpy as np
import pandas as pd
def xlement_fitting_json_input(path):
    # 将字符串路径转换为 Path 对象
    path = Path(path)
    
    # 获取文件名（不包含后缀）和后缀
    stem = path.stem
    suffix_original = path.suffix
    
    # 创建新的文件名
    new_filename = f"{stem}{'-fit'}{suffix_original}"
    new_path = path.with_name(new_filename)
    # 检查json格式
    if not check_unpredicted_json_format(path):
        print(f"{path}格式有问题")
        return

    fitting_options, df, time0, f, single_cycle_time0s = read_and_process_json(path)
    # print(f"df:{df}\ntime0:{time0}\neps:")
    origin_df = df.copy()
    if f == 103:
        r,p,i = PartialBivariate(
            data_frame=df,
            time0=time0,
            options=get_fitting_options(fitting_options),
            write_file=False,
            excel_path='FittingResultFile'
        )
        f_type = 'Partial'
    elif f == 101:
        r,p,i = LocalBivariate(
            data_frame=df,
            time0=time0,
            options=get_fitting_options(fitting_options),
            write_file=False,
            excel_path='FittingResultFile'
        )
        f_type = 'Local'
    elif f == 102:
        r,p,i = GlobalBivariate(
            data_frame=df,
            time0=time0,
            options=get_fitting_options(fitting_options),
            write_file=False,
            excel_path='FittingResultFile'
        )
        f_type = 'Global'
    elif f == 201:
        r,p,i = SingleCycleFitting3(
            data_frame=df,
            time0_dict = single_cycle_time0s,
            options=get_fitting_options(fitting_options),
        )

    # 获取 DataFrame 的行数和列数
    rows, cols = origin_df.shape

    # 获取要插入的数据的行数和列数
    new_rows, new_cols = p.shape
    # 确保新数据不会超出 DataFrame 的边界
    if new_rows > rows - 1 or new_cols > cols - 1:
        raise ValueError("新数据的大小超出了 DataFrame 的可用空间")

    # 将新数据插入到 DataFrame 中，从第二列第二行开始
    origin_df.iloc[1:1+new_rows, 1:1+new_cols] = p
    if f == 201: # 对于SingleCycle 添加nan
        origin_df.iloc[1:, 1:] = np.nan
        origin_df.iloc[1:1+new_rows, 1:1+new_cols] = p
        for col, offset in i.items():
            col_index = origin_df.columns.get_loc(col)
            # print(f"Input: col:{col_index} offset: {offset}")
            if col in origin_df.columns and offset != 0:
                # 插入 NaN 值
                nan_series = pd.Series([np.nan] * offset)
                origin_df.insert(col_index + 1, f"{col}_temp", nan_series)
                
                # 将原有值向下移动
                origin_df.iloc[offset+1:, col_index+1] = origin_df.iloc[1:-offset, col_index]
                
                # 删除临时列，更新原列
                origin_df[col] = origin_df[f"{col}_temp"]
                origin_df.drop(columns=[f"{col}_temp"], inplace=True)

    # print(f"origin_df:{origin_df}")
    process_and_save_json(
        json_file_path=path,
        df=origin_df,
        output_json_path=new_path,
        fitting_result = r
    )

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Process a file with a given path.")
    parser.add_argument("file_path", help="Path to the file to be processed")
    
    args = parser.parse_args()
    
    xlement_fitting_json_input(args.file_path)